/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.simplelabentry.web.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Role;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.hospitalcore.LabService;
import org.openmrs.module.hospitalcore.model.Lab;

public class RequireConfigurationTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7002573456863547236L;

	private final Log log = LogFactory.getLog(getClass());

	private String propertyPrefix;
	private String ignoreList;
	private String configurationPage;

	public int doStartTag() throws JspException {
		User user = Context.getAuthenticatedUser();
		LabService labService = Context.getService(LabService.class);
		List<Role> roles = new ArrayList<Role>(user.getAllRoles());
		List<Lab> labs = labService.getLabByRoles(roles);
		if ( labs == null  || labs.size() == 0 ) {
			HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
			HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
			try {
				log.info("User does not belong to any lab.");
				response.sendRedirect(request.getContextPath() + configurationPage);
				return SKIP_PAGE;
			} catch (IOException e) {
				log.warn("Unable to forward request.  It is likely that a response was already committed.  Underlying exception: " + e);
			}
		}
		HttpSession session = pageContext.getSession();
		//TODO one user can access to many labs( has many lab role )  ??
		session.setAttribute("lab", labs.get(0));
		return SKIP_BODY;
	}

	public String getPropertyPrefix() {
		return propertyPrefix;
	}

	public void setPropertyPrefix(String propertyPrefix) {
		this.propertyPrefix = propertyPrefix;
	}

	public String getIgnoreList() {
		return ignoreList;
	}

	public void setIgnoreList(String ignoreList) {
		this.ignoreList = ignoreList;
	}

	public String getConfigurationPage() {
		return configurationPage;
	}

	public void setConfigurationPage(String configurationPage) {
		this.configurationPage = configurationPage;
	}
}
